const fs = require("fs")
function readFile (err, data) {
    console.log(data.toString())
}
fs.readFile("./mock.txt", readFile)

//
class Event {
    constructor () {
        this.map = {}
    }

    add (name, fn) {
        if (this.map[name]) {
            this.map[name].push(fn)
            return this
        }

        this.map[name] = [fn]
        return this
    }

    emit (name, ...args) {
        this.map[name].forEach(fn => {
            fn(...args)
        })
        return this
    }
}

const e = new Event()
e.add("hello", (err, name) => {
    if (err) {
        console.error(err)
        return
    }
    console.log(name)
}).add("hello", (...args) => {
    console.log(...args)
}).emit("hello", "An error occured.").emit("hello", null, "hello node.js")

//
e.add("readFile", readFile)
fs.readFile("mock.txt", (err, data) => {
    e.emit("readFile", err, data)
})